#!/usr/bin/env python
# coding:utf-8
import socket
from Motor_Lidar import Motor
#from Laser_test import LaserInit
#from Power_test import PowerInit

##电量检测常量
TIME_INTERRUPT=1
ENABLE_PIN = 12
READ_PIN=11

##电机控制常量
MOROT_STEP_ANGLE = 0.9
MOTOR_DRIVER_SUBDIVIDE = 8
FIN_STEP_ANGLE = MOROT_STEP_ANGLE / MOTOR_DRIVER_SUBDIVIDE
TIME_PULSE_INTERRUPT = 0.003  # 1毫秒,实测arm最大只能到2-3ms，win最大只能十毫秒左右后期如需提速需要改程序

YAW_MAXANGLE = 720
YAW_MINIANGLE = -720
PITCH_MAXANGLE = 90
PITCH_MINIANGLE = -90

YAW_STEP_PIN = 16
YAW_DIR_PIN = 18
PITCH_STEP_PIN = 22
PITCH_DIR_PIN = 20

def tcp_client():
    motor = Motor(YAW_MAXANGLE, YAW_MINIANGLE, PITCH_MAXANGLE, PITCH_MINIANGLE,
                  FIN_STEP_ANGLE, TIME_PULSE_INTERRUPT, YAW_STEP_PIN, YAW_DIR_PIN,
                  PITCH_STEP_PIN, PITCH_DIR_PIN)
    #  开始连接
    tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 需连接的客户端ip和端口
    server_ip = "192.168.0.106"
    server_port = 2022
    # 单次传输操作
    tcp_client.connect((server_ip, server_port))
    outdata = "hello server! This is client."
    tcp_client.send(outdata.encode())
    recdata = tcp_client.recv(1024)
    print(recdata.decode())
    # 循环发送操作
    # 开始传数据
    while True:
        recdata = tcp_client.recv(1024)
        print(recdata.decode())
        if not recdata:
            print("无接受")
            break
        motor.move(float(recdata.decode()), 0)
        if motor.MotorYaw.timeflag==False:
            tcp_client.send("done".encode())
    tcp_client.close()

if __name__ == '__main__':
    print("start")
    tcp_client()